package com.sjtuy.galtxtreader.bookfile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DllDialogSplitter implements DialogSplitter{
	private Integer MaxLength;
	private Integer ExpectedLength;
	static String s=":\".!?;,：“。！？；，”‘’'";
	static String ss=":：\"“";
	
	public DllDialogSplitter(Integer m,Integer e) {
		this.MaxLength=m;
		this.ExpectedLength=e;
	}
	
	public String split(String paragraph) {
		StringBuffer sb=new StringBuffer();
		List<Integer> left=new ArrayList<Integer>();
		List<Integer> right=new ArrayList<Integer>();
		List<Boolean> isLeft=new ArrayList<Boolean>();
		Boolean flag;
		
		for(int i=0;i<paragraph.length();i++) {
			if(s.contains(paragraph.subSequence(i, i+1))) {
				flag=false;
				if(ss.contains(paragraph.subSequence(i, i+1))) flag=true;
				left.add(i);
				while(i<paragraph.length()-1 && s.contains(paragraph.subSequence(i+1, i+2))) {
					i++;
					if(ss.contains(paragraph.subSequence(i, i+1))) flag=true;
				}
				right.add(i);
				isLeft.add(flag);
			}
		}
		
		int le=0,ri,LE=0,RI,LENGTH=0;
		for(int i=0;i<left.size();i++) {
			ri=isLeft.get(i)?left.get(i):right.get(i)+1;
			LENGTH+=ri-le;
			if(LENGTH>=ExpectedLength) {
				if(LENGTH>=MaxLength) {
					sb.append(paragraph.substring(LE,le)+"\n");
					LE=le;
					LENGTH=0;
				} else {
					sb.append(paragraph.substring(LE,ri)+"\n");
					LE=ri;
					LENGTH=0;
				}
			}
			le=ri;
		}
		
		return sb.toString();
	}
}